package net

import (
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"strings"
)

func SimpleHttp() {
	http.HandleFunc("/hello", hello)
	http.ListenAndServe(":9999", nil)

}

//简单服务
func hello(res http.ResponseWriter, req *http.Request) {

	res.Header().Set("Content-Type", "text/html")
	io.WriteString(res, "<doctype html><html><head><title>Hello World</title></head><body>Hello World!</body></html>")
}

// http.Get
func HttpGet() {

	resp, err := http.Get("https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=无锡")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
}

func HttpPost() {
	resp, err := http.Post("http://www.baidu.com",
		"application/x-www-form-urlencode",
		strings.NewReader("name=abc")) // Content-Type post请求必须设置
	if err != nil {
		return
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
}
